package com.yhao.stocknew.common.utils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

    public static void deleteFile(String srcFile) {
        File file = new File(srcFile);
        file.delete();
    }

    public static boolean checkFile(String pathName, String fileName) {
        int j = 0;
        boolean sign = false;
        File file = new File(pathName);
        File[] files = file.listFiles();
        if (files != null) {
            String[] s = new String[files.length];
            for (int i = 0; i < files.length; ++i) {
                s[i] = files[i].getName();
                if (s[i].equals(fileName)) {
                    break;
                }
                ++j;
            }
            if (j == files.length)
                sign = false;
            else {
                sign = true;
            }
        }
        return sign;
    }

    public static boolean checkFile(String filePath) {
        if (filePath != null && !"".equals(filePath)) {
            File file = new File(filePath);
            return file.exists();
        } else {
            return false;
        }
    }

    public static void copyFile(String srcPath, String targetPath) {
        File f1 = new File(srcPath);
        File f2 = new File(targetPath);
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(f1);
            out = new FileOutputStream(f2);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

        } catch (Exception e) {
            // logger.error();
        } finally {
            try {
                in.close();
                out.close();
            } catch (Exception e2) {
            }
        }
    }

    /**
     * @param srcPath
     * @param targetPath
     * @return
     * @desc 拷贝文件返回目标文件
     */
    public static File copyFileRtnFile(String srcPath, String targetPath) {
        File f1 = new File(srcPath);
        File f2 = new File(targetPath);
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(f1);
            out = new FileOutputStream(f2);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

        } catch (Exception e) {
            // logger.error();
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
            } catch (Exception e2) {
            }
        }
        return f2;
    }

    public static boolean copyFileReturnBoolean(String srcFile,
                                                String targetFile) {
        boolean flag = false;
        File f1 = new File(srcFile);
        File f2 = new File(targetFile);
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(f1);
            out = new FileOutputStream(f2);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            flag = true;
        } catch (Exception e) {
        } finally {
            try {
                in.close();
                out.close();
            } catch (Exception e2) {
            }
        }
        return flag;
    }

    public static List<String> readTxt(String filePath, Charset charSet) {
        BufferedReader br = null;
        List<String> list = null;
        try {
            list = new ArrayList<String>();
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    new File(filePath)), charSet));
            String content = "";
            while ((content = br.readLine()) != null) {
                list.add(content);
            }
        } catch (Exception e) {
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (Exception e2) {
            }
        }
        return list;
    }

    /**
     * @param filePath
     * @param charset
     * @return
     * @desc 获取通篇文本
     */
    public static String getTextToString(String filePath, Charset charset) {
        List<String> texts = readTxt(filePath, charset);
        String result = "";
        for (String text : texts) {
            result += text;
        }
        return result;
    }

    public static List<String> readTxt(String filePath) {
        Charset charSet = Charset.forName("GBK");
        return readTxt(filePath, charSet);
    }

    public static void writeTxt(String filePath, List<String> contentList,
                                Charset charset) {
        if (contentList == null || contentList.size() < 1) {
            return;
        }
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(new File(filePath)), charset));
            for (int i = 0; i < contentList.size(); i++) {
                StringBuffer content = new StringBuffer().append(contentList
                        .get(i));
                if (i != contentList.size() - 1) {
                    String osName = System.getProperties().getProperty(
                            "os.name");
                    if (osName.startsWith("Windows")) {
                        content.append("\r\n");
                    } else {
                        content.append("\n");
                    }
                }
                bw.write(content.toString());
            }
            bw.flush();
        } catch (Exception e) {
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                }
            }
        }
    }

    public static void writeTxt(String filePath, List<String> contentList) {
        Charset charset = Charset.forName("GBK");
        writeTxt(filePath, contentList, charset);
    }

    public static boolean checkDirectoryIsExist(String directoryPath) {
        boolean flag = false;
        File file = new File(directoryPath);
        flag = file.isDirectory();
        return flag;
    }

    public static boolean createDirectory(String directoryPath) {
        boolean isExist = checkDirectoryIsExist(directoryPath);
        boolean flag = false;
        File file = new File(directoryPath);
        if (!isExist) {
            flag = file.mkdirs();
        } else {
            flag = true;
        }
        return flag;
    }

    /**
     * @param directoryPath
     * @return
     * @desc 创建文件夹
     */
    private static String createDirectoryNew(String directoryPath) {

        boolean flag = FileUtil.checkDirectoryIsExist(directoryPath);
        if (!flag) {
            FileUtil.createDirectory(directoryPath);
        }
        return directoryPath;
    }

    public static void appentContent(String fileName, String content) {
        RandomAccessFile randomFile = null;
        try {
            randomFile = new RandomAccessFile(fileName, "rw");
            randomFile.seek(randomFile.length());
            randomFile.write((content + "\n").getBytes("UTF-8"));
        } catch (IOException e) {
        } finally {
            try {
                randomFile.close();
            } catch (IOException e) {
            }
        }
    }
}
